﻿@page "/mainplans"
@inject NavigationManager NavigationManager
@inject IMainPlanService MainPlanService

<PageTitle>主计划列表</PageTitle>
<!--sticky固定位置，top: 3.5rem-->
<div style="position: sticky; top: 3.5rem; z-index: 1;background-color:White">
    <div class="row position-relative">
        <div class="col input-group mb-2">
            <span class="input-group-text">搜索</span>
            <input class="form-control" placeholder="Search"
                   @bind-value="@_search" @oninput="@Search" />
        </div>
        <div class="col form-check ms-3 mt-2" style="max-width: 70px">
            <input class="form-check-input" type="checkbox" value="@_annual" @onchange="OnAnnualChange">
            <label class="form-check-label">
                全年
            </label>
        </div>
        <div class="col input-group mb-2" style="max-width: 230px">
            <span class="input-group-text">月份</span>
            <input class="form-control" type="month" value="@(_selectedMonth.ToString("yyyy-MM"))" format="" @onchange="OnMonthChange"/>
        </div>
        <div class="col form-check ms-3 mt-2" style="max-width: 100px">
            <input class="form-check-input" type="checkbox" value="@_fromMonth"
                   checked="checked" @onchange="OnFromMonthChange">
            <label class="form-check-label">
                本月往后
            </label>
        </div>
        <div class="col mt-2">
            @if (_mainPlanDtos != null)
           {
               <span class="mx-3">Project Qty:@_mainPlanDtos.Count</span>
           }
       </div>
       <CompAddButton Roles="admin,pmc,pm" Url="mainplan/add"/>
    </div>
</div>

<table class="table table-hover table-striped" style="table-layout: fixed; word-break: break-all;">
    <!--sticky固定位置，top: 6rem-->
    <thead style="position: sticky; top: 6rem; z-index: 1; background-color: White">
    <tr>
        
        <th style="width: 70px">分批</th>
        <th style="width: 10%">编号</th>
        <th style="width: 10%">绑定项目</th>
        <th >项目名称</th>
        <th style="width: 7%">数量</th>
        <th >型号</th>
        <th style="width: 7%">创建时间</th>
        <th style="width: 7%">完工时间</th>
        <th style="width: 7%">计划发图</th>
        <th style="width: 7%; text-align: center">类型</th>
    </tr>
    </thead>
    <tbody>
        @if (_filterMainPlanDtos.Count == 0)
    {
        <p>...</p>
    }
    else
    {
            @foreach (var plan in _filterMainPlanDtos)
        {
            <MainPlanRow MainPlanDto="plan"/>
        }
    }
    </tbody>
</table>

@code {
    //过滤参数
    private string _search = string.Empty;
    private bool _annual;
    private bool _fromMonth=true;
    private DateTime _selectedMonth = DateTime.Today;
    private int _year;
    private int _month;
    private List<MainPlanDto> _mainPlanDtos = new();
    private List<MainPlanDto> _filterMainPlanDtos = new();

    protected override async Task OnParametersSetAsync()
    {
        _year= _selectedMonth.Year;
        _month= _selectedMonth.Month;

        await GetData();
        Search(new ChangeEventArgs { Value = "" });
        StateHasChanged();
    }

    //过滤条件变化
    private async void OnAnnualChange(ChangeEventArgs e)
    {
        _annual =Convert.ToBoolean(e.Value);
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async void OnFromMonthChange(ChangeEventArgs e)
    {
        _fromMonth =Convert.ToBoolean(e.Value);
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async void OnMonthChange(ChangeEventArgs e)
    {
        _selectedMonth = Convert.ToDateTime(e.Value);
        _year= _selectedMonth.Year;
        _month= _selectedMonth.Month;
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async Task GetData()
    {
        _mainPlanDtos.Clear();
        _mainPlanDtos.AddRange(await MainPlanService.GetBlazorMainPlansAsync(_year, _month, _annual, _fromMonth,false,false));
    //异步方法需要主动告知前台状态已经发生改变
        StateHasChanged();
    }
    
    //过滤逻辑
    private void Search(ChangeEventArgs e)
    {
        //将搜索放在外面做（4.没搜索||5.搜索项目号||6.搜索项目名称）
        _search=e.Value!.ToString()!;
        _filterMainPlanDtos.Clear();
        _filterMainPlanDtos.AddRange(_mainPlanDtos.Where(
            x => string.IsNullOrEmpty(_search) ||
                 x.Number.ToLower().Contains(_search.ToLower()) ||
                 x.Name.ToLower().Contains(_search.ToLower())));

        StateHasChanged();
    }
}
